﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.TabView = function(element) {
    YUIAspNet.TabView.initializeBase(this, [element]);
}

YUIAspNet.TabView.prototype = {
    initialize: function() {
        YUIAspNet.TabView.callBaseMethod(this, 'initialize');
        //Sys.Application.add_init(Function.createDelegate(this, this.initializeTabView));
    },

    initializeYUIControl: function(sender, args) {

        var tabView = new YAHOO.widget.TabView({ orientation: this.get_Orientation() });

        for (var i = 0; i < this.get_Tabs().length; i++) {
            tabView.addTab(new YAHOO.widget.Tab({
                label: this.get_Tabs()[i].Text,
                active: this.get_Tabs()[i].Active,
                contentEl: $get(this.get_Tabs()[i].ID),
                disabled: !this.get_Tabs()[i].Enabled
            }));
        }

        if (this._events) {
            var beforeChangeHandler = this._events.getHandler("onBeforeChange");
            var sender = this;

            if (beforeChangeHandler) {
                tabView.on("beforeActiveTabChange",
                    function(yuiArgs) {
                        args = { cancel: false, prevTab: yuiArgs.prevValue, newTab: yuiArgs.newValue };

                        beforeChangeHandler(sender, args);

                        if (args.cancel)
                            return false;
                    });
            }

            var afterChangeHandler = this._events.getHandler("onAfterChange");
            var autopostback = this.get_AutoPostback();

            if (afterChangeHandler) {
                tabView.on("activeTabChange",
                    function(yuiArgs) {
                        args = { prevTab: yuiArgs.prevValue, newTab: yuiArgs.newValue };

                        afterChangeHandler(sender, args);

                        if (autopostback) {
                            sender.DoPostback(tabview.getTabIndex(yuiArgs.newValue));
                        }
                        else { //update active tab..
                            $get(sender.get_ActiveTabControlID()).value = tabView.getTabIndex(yuiArgs.newValue);
                        }
                    });
            }
            else if (autopostback) {
                tabView.on("activeTabChange",
                    function(yuiArgs) {
                        sender.DoPostback(tabView.getTabIndex(yuiArgs.newValue));
                    });
            }
            else {
                tabView.on("activeTabChange",
                    function(yuiArgs) {
                        $get(sender.get_ActiveTabControlID()).value = tabView.getTabIndex(yuiArgs.newValue);
                    });
            }
        }

        tabView.appendTo(this.get_element().id);

        this._yuiControl = tabView;
    },

    dispose: function() {
        $clearHandlers(this.get_element());
        YUIAspNet.TabView.callBaseMethod(this, 'dispose');
    },

    get_Tabs: function() {
        return this._tabs;
    },
    set_Tabs: function(tabs) {
        this._tabs = tabs;
    },

    get_ActiveTabControlID: function() {
        return this._activeTabControlID;
    },
    set_ActiveTabControlID: function(activeTabControlID) {
        this._activeTabControlID = activeTabControlID;
    },

    get_AutoPostback: function() {
        return this._autoPostback;
    },
    set_AutoPostback: function(autoPostback) {
        this._autoPostback = autoPostback;
    },

    get_Orientation: function() {
        return this._orientation;
    },
    set_Orientation: function(orientation) {
        this._orientation = orientation;
    },

    add_onBeforeChange: function(handler) {
        this.get_events().addHandler("onBeforeChange", handler);
    },
    remove_onBeforeChange: function(handler) {
        this.get_events().removeHandler("onBeforeChange", handler);
    },

    add_onAfterChange: function(handler) {
        this.get_events().addHandler("onAfterChange", handler);
    },
    remove_onAfterChange: function(handler) {
        this.get_events().removeHandler("onAfterChange", handler);
    }
}

YUIAspNet.TabView.registerClass('YUIAspNet.TabView', YUIAspNet.BaseControl);
